package day9;
//通过synchronized 模拟多线程 轮流并发执行
//假设三个线程A,B,C 要求顺序是ABCABC...
//2.借助于它wait()和notify()实现
public class Demo6 implements Runnable{
    int id;
    static int count;
    static int len;//线程的个数
    public Demo6(int id){
        this.id=id;
    }
    public void run() { //动态了
        String name=Thread.currentThread().getName();
        while(true){
            synchronized (Demo6.class){
                if (count % len == id) {
                    System.out.println(name);
                    count++;
                    Demo6.class.notifyAll();
                }
                try {
                    Demo6.class.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) {
        Demo6.len=4;
        for(int i=0;i<Demo6.len;i++) {
            new Thread(new Demo6(i), (char)(65+i)+"")
                    .start();
        }
    }
}
